//
//  LoginCtr.swift
//  travel
//
//  Created by tong on 16/7/15.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import MBProgressHUD
import Eelay





public struct EeLogin
{
    public static var phone = "^1+[123456789]+\\d{9}$"
    public static var globalPhone = "^\\d{1,6}|\\d{2,20}$"
    public static var password = "^\\S{1,32}$"
    public static var code = "^\\S{1,100}"
    public static var email = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
    
    public static var __main_hex = "#e67e22"
}



open class EeLoginCtr:CoInputController
{
    
    public var usernameT = JoTextField(id: "username", defaultV: nil, need: true, hiden: false, rule: JoRule(reg: EeLogin.phone, nil_msg: "请输入手机号".loc, err_msg: "检查手机号".loc))
    
    public var passwordT = JoTextField(id: "password", defaultV: nil, need: true, hiden: false, rule: JoRule(reg: EeLogin.password, nil_msg: "请输入密码".loc, err_msg: "检查密码".loc))
    
    public var firstLoad = true
    
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        layoutView()
    }
    
    open func layoutView() {
        self.title = ""
        self.top_style = .transparent_dark
        
        inputBack.eelay = [
            [cornerV,280,[ee.X.Y,[0,-100]]],
        ]
        
        usernameT.delegate = self
        passwordT.delegate = self
        
        usernameT.placeholder = "电话号码".loc
        passwordT.placeholder = "请输入密码".loc
        passwordT.isSecureTextEntry = true
        
        inputBack.backgroundColor = .white
        
        _ = usernameT.ui.font16
        _ = passwordT.ui.font16
        
        let line1 = UIView()
        line1.bsui.background(hex:"#666" )
        
        
        let logoL = UILabel()
        
        let bkview = UIView()
        cornerV.eelay = [
            [logoL,[ee.T.X]],
            [bkview,[logoL,ee.B,ee.T,20],[ee.B.L.R]],
            [usernameT,[ee.L.R,[16,-16]],"50",[logoL,ee.B,ee.T,20]],
            [passwordT,[ee.B.L.R,[0,16,-16]],"50",[usernameT,ee.B,ee.T,0]],
            [line1,[ee.L.R,[0,0]],[usernameT,ee.B],"0.5"],
        ]
        
        bkview.bsui.corner5.background(hex: "#fff")
        bkview.bsui.set(radius: 1, width: 1, color: UIColor(shex:"#aaa"))
        _ = logoL.ui.font48.cl_666.text("IWE")
        
        
        usernameT.ui.font15.text(hex: "#666")
        passwordT.ui.font15.text(hex: "#666")
        usernameT.tintColor = UIColor(shex: "#29BC3E")
        passwordT.tintColor = UIColor(shex: "#29BC3E")
        
        usernameT.setPlaceholder(info: ("请输入手机号".loc,UIColor(shex:"#aaa"),UIFont.systemFont(ofSize: 14)))
        passwordT.setPlaceholder(info: ("请输入密码".loc,UIColor(shex:"#aaa"),UIFont.systemFont(ofSize: 14)))
        
        let loginBt = UIButton()
        let registerBt = UIButton()
        let pwdBt = UIButton()
        inputBack.eelay = [
            //            [pwdBt,[cornerV,ee.R],[cornerV,ee.B,ee.T,8]],
            [loginBt,"40",200,[ee.X],[cornerV,ee.B,ee.T,40]],
            [registerBt,"40",200,[ee.X.B,[0,-10]]],
        ]
        
        
        cornerV.backgroundColor = .clear
        _ = loginBt.ui.buttonColor(hex:EeLogin.__main_hex).ncolor(hex: "#fff").ntitle(title: "登录").font(size: 17).corner3
        //        _ = loginBt.ui.ncolor(hex: "#fff").ntitle(title: "登录").font(size: 17).set(radius: 3, width: 1, color: .white)
        _ = registerBt.ui.ncolor(hex: "#aaa").ntitle(title: "注册新用户".loc).font(size: 14)
        loginBt.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        registerBt.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        pwdBt.ui.ncolor(hex: "#fff").ntitle(title: "\("忘记密码".loc)>>").bfont(size: 16)
        pwdBt.addTarget(self, action: #selector(findPwdAction), for: .touchUpInside)
        
        
        let bundle = Bundle(for: EeLoginCtr.self)
        let canleImg = UIImage(named: "ali_delete", in: bundle, compatibleWith: nil)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: canleImg, style: .plain, target: self, action: #selector(canle))
        //        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.plain, target: self, action: #selector(canle))
        
        //        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.plain, target: self, action: #selector(register))
        weak var wself = self
        EeRegisterCtr.success_register = {
            let hud = MBProgressHUD.showAdded(to: self.navigationController!.view, animated: true)
            let img = UIImage(named:"Checkmark")?.withRenderingMode(.alwaysTemplate)
            let imgV = UIImageView(image: img)
            hud.customView = imgV
            hud.mode = .customView
            hud.minSize = [150,100]
            hud.label.text = "注册成功".loc
            
            wself?.usernameT.text = $0["username"] as? String
            wself?.passwordT.text = $0["password"] as? String
            weak var wself = self
            co_delay(delay: 1, closure: {
                wself?.loginAction()
                
            })
        }
        
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if firstLoad
        {
            if fastRigister{
                let ctr = EeRegisterCtr()
                self.navigationController?.pushViewController(ctr, animated: false)
            }
            firstLoad = false
        }
    }
    
    
    
    @objc open func loginAction() {
        let info  = JoPaw.paw(fields: usernameT,passwordT)
        currentT.resignFirstResponder()
        if info.validate
        {
            
            weak var wself = self
            var hud:MBProgressHUD? = MBProgressHUD.init(view: wself!.navigationController!.view)
            
            if hud == nil{
                hud = MBProgressHUD.showAdded(to: wself!.navigationController!.view, animated: true)
            }
            hud?.minSize = [150,100]
            hud?.label.text = "正在登录".loc
            hud?.mode = .indeterminate
            co_delay(delay: 1.0, closure: {
                let info = info.values.mutable_dictionary
                let user = JoUser()
                user.info = info
                user.login()
                NotificationCenter.default.post(name: Notification.Name(rawValue: "LoginAction"), object: nil, userInfo: nil)
                
                if let c_user = JoUser.loginUser()
                {
                    wself?.navigationController?.login_block?(false,"登录成功".loc,nil)
                }
                else
                {
                    hud?.hide(animated: true)
                    debugPrint("net login success but have no  logined JoUser")
                }
            })
            
            
        }
    }
    
    @objc open func findPwdAction() {
        //        let ctr = PwdFixCtr()
        //        self.navigationController?.pushViewController(ctr, animated: true)
    }
    
    @objc open func register()  {
        let ctr = EeRegisterCtr()
        self.navigationController?.pushViewController(ctr, animated: true)
    }
    
    @objc open func canle() {
    
        navigationController?.login_block?(false,"取消登录".loc,nil)
        self.navigationController?.dismiss(animated: true, completion: { () -> Void in})
    }
    
    deinit {
    }
    
}

